package me.seu.demo.service.json;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * json tsl 解析工具
 *
 * @author liangfeihu
 * @since 2021/5/28 下午4:18
 */
public class TslJsonParser {

    public static void main(String[] args) throws IOException {
        String jsonFilePath = "/Users/a123/codes/myown/learning-demo/common-demo/src/main/resources/product_tsl.json";
        File file = new File(jsonFilePath );
        String inputJson = FileUtils.readFileToString(file,"UTF-8");

        System.out.println(inputJson);

        System.out.println("----------------");

        System.out.println(JSONObject.toJSON(null));

        Tsl tsl = parseTsl(inputJson);

        System.out.println(tsl.toString());


    }

    public static Tsl parseTsl(String tslJson) {
        // 物模型json解析
        JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(tslJson);
        Tsl.Profile profile = jsonObject.getObject("profile", Tsl.Profile.class);
        List<Tsl.Property> properties = jsonObject.getObject("properties", List.class);
        List<Tsl.Event> events = jsonObject.getObject("events", List.class);
        List<Tsl.Service> services = jsonObject.getObject("services", List.class);
        Tsl tsl = new Tsl();
        tsl.setProfile(profile);
        tsl.setProperties(properties);
        tsl.setEvents(events);
        tsl.setServices(services);
        return tsl;
    }

}
